package com.juzipi.test;

import com.juzipi.MajorApplication;
import com.juzipi.framework.redis.RedisLock;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

@SpringBootTest(classes = MajorApplication.class)
public class RedisTestLock {


    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    private void testLock(String batch) throws InterruptedException {
        //加锁
        RedisLock lock = RedisLock.newLock(redisTemplate, "testRedisLock");
        try {
            if (lock.tryLock()) {
                System.out.println("加锁成功_"+batch);
                Thread.sleep(2000);
            }
            System.out.println("加锁失败_"+batch);
        } finally {
            //解锁
            lock.release();
            System.out.println("解锁成功"+batch);
        }
    }

    @Test
    public void testLock1() throws InterruptedException {
        testLock("1");
    }


    @Test
    public void testLock2() throws InterruptedException {
        testLock("2");
    }

}
